package com.example.wakaka.locknlock;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText textname;
    private EditText textpwd;
    private EditText textpwd2;
    RadioGroup radioGroup;
    Button okbut ;
    Button clearbut;
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textname = (EditText)findViewById(R.id.editname);
        textpwd = (EditText)findViewById(R.id.editpassword);
        textpwd2 = (EditText)findViewById(R.id.editpassword2);
        radioGroup = (RadioGroup)findViewById(R.id.radiogroup);
        okbut = (Button) findViewById(R.id.buttonOK);
        clearbut = (Button) findViewById(R.id.buttonClear);
        imageView = (ImageView)findViewById(R.id.image);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int index = radioGroup.getCheckedRadioButtonId();
                RadioButton tempbut = (RadioButton)findViewById(index);
                String text = tempbut.getText().toString();
                if(text.equals("Login"))
                {
                    //imageView.setVisibility(View.INVISIBLE);
                    textpwd2.setVisibility(View.INVISIBLE);
                    textpwd.setHint("Password");

                }
                else if(text.equals("Register"))
                {
                    //imageView.setVisibility(View.VISIBLE);
                    textpwd2.setVisibility(View.VISIBLE);
                    textpwd.setHint("New Password");

                }
                textpwd.setText("");
                textpwd2.setText("");
            }
        });
        okbut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int index = radioGroup.getCheckedRadioButtonId();
                RadioButton tempbut = (RadioButton)findViewById(index);
                String text = tempbut.getText().toString();
                String name = textname.getText().toString();
                String password1 = textpwd.getText().toString();
                String password2 = textpwd2.getText().toString();
                if(text.equals("Login"))
                {
                    myDB db = new myDB(getBaseContext());
                    String correct = db.search(name);
                    if(name.equals(""))
                    {
                        Toast.makeText(MainActivity.this,"name can't be empty",Toast.LENGTH_LONG).show();
                    }
                    else if(correct.equals(""))
                    {
                        Toast.makeText(MainActivity.this,"not existed name",Toast.LENGTH_LONG).show();
                    }
                    else if(password1.equals(correct))
                    {
                        Toast.makeText(MainActivity.this,"login success",Toast.LENGTH_LONG).show();
                        //登陆成功界面跳转
                        Intent intent = new Intent(MainActivity.this, IndexActivity.class);
                        intent.putExtra("username",name);
                        startActivityForResult(intent, 1);
                    }
                    else
                    {
                        Toast.makeText(MainActivity.this,"wrong password",Toast.LENGTH_LONG).show();
                    }
                }
                else if(text.equals("Register"))
                {
                    myDB db = new myDB(getBaseContext());
                    String correct = db.search(name);
                    if(!correct.equals(""))
                    {
                        Toast.makeText(MainActivity.this,"name already existed",Toast.LENGTH_LONG).show();
                        return;
                    }
                    if(name.equals(""))
                    {
                        Toast.makeText(MainActivity.this,"name can't be empty",Toast.LENGTH_LONG).show();
                        return;
                    }
                    if(password1.equals(""))
                    {
                        Toast.makeText(MainActivity.this,"password can't be empty",Toast.LENGTH_LONG).show();
                        return;
                    }
                    if(!password1.equals(password2))
                    {
                        Toast.makeText(MainActivity.this,"password not match",Toast.LENGTH_LONG).show();
                        return;
                    }

                    db.insert(name,password1);
                    Toast.makeText(MainActivity.this,"register success",Toast.LENGTH_LONG).show();
                    //注册成功跳转
                    Intent intent = new Intent(MainActivity.this, IndexActivity.class);
                    intent.putExtra("username",name);
                    startActivityForResult(intent, 1);
                }
            }
        });
        clearbut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textname.setText("");
                textpwd.setText("");
                textpwd2.setText("");
            }
        });

    }
}
